layui.use(['table', 'admin', 'ax','ztree','func','tree','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var $ZTree = layui.ztree;
    var func = layui.func;
    var tree = layui.tree;
    /**
     * 装修货品表管理
     */
    var ZxHuopin = {
        tableId: "zxHuopinTable",
        btyZxKemuId:"",
        btyZxKemuIdView:"",
        condition:{
            btyZxKemuIds:""
        }
    };

    /**
     * 初始化表格的列
     */
    ZxHuopin.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'btyZxHuopinId', hide: true, title: '主键'},
            // {field: 'ghs_view', sort: true, title: '供货商'},
            {field: 'name', sort: true, title: '名称',edit: 'text'},
            {field: 'guige', sort: true, title: '规格',edit: 'text'},
            {field: 'danwei', sort: true, title: '单位',edit: 'text'},
            {field: 'danjia', sort: true, title: '单价',edit: 'text'},
            // {field: 'code', sort: true, title: '编码'},
            // {field: 'zhujima', sort: true, title: '助记码'},
            // {field: 'btyZxKemuId', sort: true, title: '所属科目'},
            // {field: 'btyZxKemuIdView', sort: true, title: '所属科目名称'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    table.on('edit(zxHuopinTable)', function(obj){
        var value = obj.value, //得到修改后的值
            data = obj.data, //得到所在行所有键值
            field = obj.field; //得到字段
        $.ajax({
            type: "PUT",
            url: "/dgdzxHuopin/editItemGhs",
            datatype: "json",
            data: data,
            success: function (data) {
                table.reload(ZxChengben.tableId);
            }, error: function () {
                Feng.error("修改失败！");
            }, complete: function (XHR, TS) { }
        });
    });

    /**
     * 点击查询按钮
     */
    ZxHuopin.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        table.reload(ZxHuopin.tableId, {where: queryData});
    };

    layui.form.on('submit(btnSubmit)', function () {
        ZxHuopin.hpAdd();
    });
    ZxHuopin.hpAdd = function () {
        var ajax = new $ax(Feng.ctxPath + "/dgdzxHuopin/addItemGhs", function (data) {
            if (data.code == 1) {
                Feng.error("添加失败!" + data.message);
                return;
            }
            Feng.success("添加成功！");
            $("#name").val("");
            table.reload(ZxHuopin.tableId);
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set("name",$("#name").val());
        ajax.set("code",$("#code").val());
        ajax.set("ghs",$("#ghs").val());
        ajax.set("ghs_view",$("#ghs_view").val());
        ajax.set("ghsIds",$("#ghs").val());
        ajax.set("ghsNames",$("#ghs_view").val());
        ajax.set("guige",$("#guige").val());
        ajax.set("danwei",$("#danwei").val());
        ajax.set("danjia",$("#danjia").val());
        ajax.set("btyZxKemuId",ZxHuopin.btyZxKemuId);
        ajax.set("btyZxKemuIdView",ZxHuopin.btyZxKemuIdView);
        ajax.start();

        return false;
    }

    var codetext = document.getElementById("name");
    codetext.addEventListener("keyup", function(event) {
        event.preventDefault();
        if (event.keyCode === 13) {
           var naval= $("#name").val();
            if (naval.trim()=="") {
                Feng.error("货品名称不能为空");
                return false;
            }
            // layui.form.on('submit(btnSubmit)', function (data) {
                ZxHuopin.hpAdd();
            // });
        }
    });

    /**
     * 导出excel按钮
     */
    ZxHuopin.exportExcel = function () {
        var checkRows = table.checkStatus(ZxHuopin.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ZxHuopin.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/dgdzxHuopin/edit?btyZxHuopinId=' + data.btyZxHuopinId;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ZxHuopin.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dgdzxHuopin/delete", function (data) {
                if(data.code==200){
                    Feng.success("删除成功!");
                    table.reload(ZxHuopin.tableId);
                }else {
                    Feng.error(data.message);
                }
            });
            ajax.set("btyZxHuopinId", data.btyZxHuopinId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ZxHuopin.tableId,
        url: Feng.ctxPath + '/dgdzxHuopin/list',
        page: true,
        height: "full-95",
        cellMinWidth: 100,
        where:{
            ghs:$("#ghs").val()
        },
        cols: ZxHuopin.initColumn()
    });

    var ZxKemu = { };
    /**
     * 选择科目时
     */
    ZxKemu.onClickNode = function (obj) {
        ZxHuopin.btyZxKemuId=obj.data.id;
        ZxHuopin.btyZxKemuIdView=obj.data.title;
        if(obj.data.pid==-1){//如果是顶级
            var firsts=obj.data.children;
            var seconds="";
            for(var i=0;i<firsts.length;i++){
                seconds+=ZxKemu.getchilds(firsts[i].children);
            }
            ZxHuopin.condition.btyZxKemuIds=seconds.substr(0,seconds.length-1);
        }else {

            var childs=obj.data.children;
            if(childs.length==0){
                ZxHuopin.condition.btyZxKemuIds = obj.data.id;
            }else{
                var ids=ZxKemu.getchilds(childs);
                ZxHuopin.condition.btyZxKemuIds=ids.substr(0,ids.length-1);
            }
        }
        ZxHuopin.search();
    };
    ZxKemu.getchilds = function getchilds(childs) {
        var ids="";
        for(var i=0;i<childs.length;i++){
            ids+=childs[i].id+",";
        }
        return ids;
    }
    // //初始化左侧门店树
    // var ajax = new $ax(Feng.ctxPath + "/zxKemu/layuiTree", function (data) {
    //     tree.render({
    //         elem: '#kemuTree',
    //         data: data,
    //         click: ZxKemu.onClickNode,
    //         onlyIconControl: true
    //     });
    // }, function (data) {
    // });
    // ajax.start();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ZxHuopin.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ZxHuopin.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ZxHuopin.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ZxHuopin.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ZxHuopin.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ZxHuopin.onDeleteItem(data);
        }
    });
});

$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});
